<?php

namespace App\Library\Tencent\TencentCloud;

use Exception;

class TencentCloudApi extends BaseTencentCloudApi
{
    /**
     * 上传文件
     *
     * @param $params
     * @return array
     */
    public function uploadFile($params): array
    {
        $file = file_get_contents($params['file']);

        $this->createClient();
        $result = $this->client->putObject([
            'Bucket' => $this->bucket,
            'Key'    => $params['filename'],
            'Body'   => $file,
        ]);

        return $result->toArray();
    }

    /**
     * 获取上传token
     *
     * @param $pathPrefix
     * @return array
     * @throws Exception
     */
    public function getUploadToken($pathPrefix): array
    {
        $accessToken = $this->getTempKeys($pathPrefix);

        return [
            'domain'       => $this->domain,
            'token'        => $accessToken['accessToken'],
            'region'       => $this->region,
            'tmpSecretId'  => $accessToken['tmpSecretId'],
            'tmpSecretKey' => $accessToken['tmpSecretKey'],
            'bucket'       => $this->bucket,
            'startTime'    => time(),
            'expiredTime'  => $accessToken['expiredTime'],
            'pathPrefix'   => $pathPrefix,
        ];
    }

}
